<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Http\FormRequest;

/**
 * App\Models\UserGlobal
 *
 * @property int $uid 用户id
 * @property int $global_status 用户流程状态
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserGlobal newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserGlobal newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserGlobal query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserGlobal whereGlobalStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserGlobal whereUid($value)
 * @mixin \Eloquent
 * @property string|null $last_request_fail_time 用户最新一次授信失败日期
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserGlobal byUid($uid)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserGlobal whereLastRequestFailTime($value)
 * @property string $jpush_registration_id 极光推送注册id
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserGlobal whereJpushRegistrationId($value)
 * @property int|null $new_user 用户新老状态 1=新用户 2=老用户
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserGlobal whereNewUser($value)
 */
class UserGlobal extends Model
{

    /**
     * 黑名单
     * @var array
     */
    protected $guarded = [];

    /**
     * 禁用时间戳
     * @var bool
     */
    public $timestamps = false;
    /**
     * 默认初始化首页
     */
    const STATUS_NOTHING = 0;

    /**
     * 借款已提交审核
     */
    const STATUS_LOAN_REQUEST = 1;

    /**
     * 借款审核未通过
     */
    const STATUS_LOAN_REQUEST_FAILED = 2;

    /**
     * 借款审核通过
     */
    const STATUS_LOAN_REQUEST_SUCCESS = 3;

    /**
     * 收款超时
     */
    const STATUS_LOAN_TIME_OUT = 4;

    /**
     * 收款完全超时
     */
    const STATUS_LOAN_RESTART = 5;

    /**
     * 正在放款中
     */
    const STATUS_LOAN_MONEY_PROCESSING = 40;

    
    /**
     * 放款失败
     */
    const STATUS_LOAN_MONEY_FAIL = 45;

    /**
     * 放款失败，需要换卡
     */
    const STATUS_LOAN_CHANGE_CARD =50;

    /**
     * 还款初始
     */
    const STATUS_REPAY_INIT = 61;

    /**
     * 还款中
     */
    const STATUS_REPAY_ING = 65;

    /**
     * 还款成功
     */
    const STATUS_REPAY_SUCCESS = 70;

    /**
     * 还款失败
     */
    const STATUS_REPAY_FAILURE = 75;

    /**
     * 全部结清
     */
    const STATUS_REPAY_OVER = 80;



    /**
     * Description:定义uid查询作用域
     * Author: Sam
     * DateTime: 2019/1/6 21:16
     * @param $query
     * @param $uid
     * @return mixed
     */
    public function scopeByUid($query,$uid)
    {
        return $query->where('uid', $uid);
    }

    /**
     * Description: 用户极光数据相关更新
     * Author: Gxs
     * @param User $user
     * @param FormRequest $request
     * @return bool
     */
    public static function jpushDataUpdate(User $user, $request)
    {
        $registration_id = $request->input('registration_id');
        if (!empty($registration_id))
            UserGlobal::where('uid', $user->id)->update([
                'jpush_registration_id' => $registration_id
            ]);
        return true;
    }
}
